<?php

namespace Bigspender\DefaultBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Bigspender\DefaultBundle\Entity\Movimentation;
use Bigspender\DefaultBundle\Form\MovimentationType;


class MovimentationController extends Controller
{
	/**
	 * @Route("/movimentation/create", name="_create_movimentation")
	 */
	public function createAction(Request $request)
	{
		$movimentation = new Movimentation();
		$form = $this->createForm(new MovimentationType(), $movimentation);
		$form->handleRequest($request);
		$translator = $this->get('translator');
		$flashBag = $this->get('session')->getFlashBag();

		if ($form->isValid()) {
			try {
				$movimentation->setUser($this->get('security.context')->getToken()->getUser());
				$em = $this->getDoctrine()->getManager();
				$em->persist($movimentation);
				$em->flush();
				
				$flashBag->setName('success');
				$flashBag->add('message', $translator->trans('CREATE MOVIMENTATION SUCCESS'));
				return $this->redirect($this->generateUrl('_index'));
			}
			catch (\Exception $e) {
				$flashBag->setName('error');
				$flashBag->add('message', $translator->trans('CREATE MOVIMENTATION SYSTEM ERROR'));                    
			}
		}
		else if ($form->isSubmitted()) {
			$flashBag->setName('error');
			$flashBag->add('message', $translator->trans('CREATE MOVIMENTATION FORM ERROR'));                    			
		}

		return $this->render('BigspenderDefaultBundle:Movimentation:create.html.twig', array('form' => $form->createView()));
	}
}
